﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WhatSNew.AppCode;

namespace WhatSNew.RegisterAndSign
{
    public partial class Register : System.Web.UI.Page
    {
        private bool nameIsOK;
        private bool pwdIsOK;
        private bool emailIsOK;
        protected void Page_Load(object sender, EventArgs e)
        {
            nameIsOK = false;
            pwdIsOK = false;
            emailIsOK = false;
        }

        protected void RegisterBtn_Click(object sender, EventArgs e)
        {
            UserInfoAccess infoAccess = new UserInfoAccess();
            if (TB_Name.Text.Trim().Length == 0)
            {
                LB_Name.Text = "用户名不能为空（格）";
            }
            else if (infoAccess.IsExist(TB_Name.Text) > 0)
            {
                LB_Name.Text = "用户名已存在";
            }
            else
            {
                LB_Name.Text = "用户名可用";
                nameIsOK = true;
            }

            if (TB_Pwd.Text.Length < 6)
            {
                LB_Password.Text = "密码长度须不少于6位";
            }
            else if (TB_RePwd.Text.Equals(TB_Pwd.Text))
            {
                pwdIsOK = true;
                //LB_RePassword.Text = "密码一致";
            }
            else
            {
                LB_RePassword.Text = "两次密码不一致";
            }

            if (TB_Email.Text.Length != 0 && !REV_Email.IsValid)
            {
                LB_Email.Text = "邮件格式不对";
            }
            else
            {
                emailIsOK = true;
                LB_Email.Text = "邮件格式正确";
            }

            if (!(nameIsOK && pwdIsOK && emailIsOK))
                return;
            UserInfo newUser = new UserInfo();
            newUser.PInfo.Name = TB_Name.Text;
            newUser.PInfo.Password = TB_Pwd.Text;
            infoAccess.Insert(newUser);
            Response.Redirect("SignIn.aspx");
        }
    }
}